import java.net.*;
import java.io.*;

public class KnockKnockServer{

    //Very simple server class just a demonstration
    public static void main(String[] args) throws IOException{

        ServerSocket server_socket = null;

        try{
            server_socket = new ServerSocket(4444);

        }
        catch(IOException e){
            
            System.err.println("Problem in creating server socket in 4444");
            System.exit(1);

        }
        
        //Now create your client socket here
        Socket client_socket = null;

        try{
            //sleep here and wait for the data
            client_socket = server_socket.accept();

        }
        catch(IOException e){
            System.err.println("Error in accepting data from client");
            System.exit(1);
        }

        //you can write you data to that channel with easiness cool!
        PrintWriter out = new PrintWriter(client_socket.getOutputStream(),true);
        BufferedReader in = new BufferedReader(
                new InputStreamReader(client_socket.getInputStream()));

        String inputLine,outputLine; // you can read or write to these and send data over the network

        //the stupid protocol here
        KnockKnockProtocol kkp = new KnockKnockProtocol();
        outputLine = kkp.processInput(null);
        out.println(outputLine); // send over netwok

        while((inputLine= in.readLine())!=null){ //while you get info from the network
            outputLine = kkp.processInput(inputLine);
            out.println(inputLine);

            if (outputLine.equals("See"))
                System.out.println("Shutting down the server application");
                break ;
        
        }

        //sen copunu kullan baskasi almasin ya
        out.close();
        in.close();
        client_socket.close();
        server_socket.close();
    }
}
